package com.ec.pf.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ec.pf.pojo.BindingInfo;
import com.ec.pf.pojo.FamilyElderlyBinding;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

@Mapper
public interface FamilyElderlyBindingMapper extends BaseMapper<FamilyElderlyBinding> {
    // 根据家属姓名和老人姓名模糊查询绑定关系
    @Select("SELECT feb.id, feb.family_user_id, feb.elderly_user_id, feb.status, " +
            "fp.full_name AS family_name, fp.phone AS family_phone, fp.relationship AS family_relationship, " +
            "ep.full_name AS elderly_name, ep.id_card_number AS elderly_id_card " +
            "FROM family_elderly_bindings feb " +
            "LEFT JOIN family_profiles fp ON feb.family_user_id = fp.user_id " +
            "LEFT JOIN elderly_profiles ep ON feb.elderly_user_id = ep.user_id " +
            "WHERE fp.full_name LIKE CONCAT('%', #{familyName}, '%') " +
            "AND ep.full_name LIKE CONCAT('%', #{elderlyName}, '%')")
    Page<BindingInfo> selectBindingWithDetailsPaged(Page<BindingInfo> page,
                                                    @Param("familyName") String familyName,
                                                    @Param("elderlyName") String elderlyName);

    // 查询绑定记录，返回所有相关信息
    @Select("SELECT feb.id, feb.family_user_id, feb.elderly_user_id, feb.status, " +
            "fp.full_name AS family_name, fp.phone AS family_phone, fp.relationship AS family_relationship, " +
            "ep.full_name AS elderly_name, ep.id_card_number AS elderly_id_card " +
            "FROM family_elderly_bindings feb " +
            "LEFT JOIN family_profiles fp ON feb.family_user_id = fp.user_id " +
            "LEFT JOIN elderly_profiles ep ON feb.elderly_user_id = ep.user_id")
    Page<BindingInfo> selectBindingAllPaged(Page<BindingInfo> page);

    // 家属分页查询
    @Select("SELECT feb.id, feb.family_user_id, feb.elderly_user_id, feb.status, " +
            "fp.full_name AS family_name, fp.phone AS family_phone, fp.relationship AS family_relationship, " +
            "ep.full_name AS elderly_name, ep.id_card_number AS elderly_id_card " +
            "FROM family_elderly_bindings feb " +
            "LEFT JOIN family_profiles fp ON feb.family_user_id = fp.user_id " +
            "LEFT JOIN elderly_profiles ep ON feb.elderly_user_id = ep.user_id " +
            "WHERE feb.family_user_id = #{familyUserId}")
    Page<BindingInfo> selectSelfBindingFamilyPaged(Page<BindingInfo> page, @Param("familyUserId") Long familyUserId);

    @Select("SELECT feb.id, feb.family_user_id, feb.elderly_user_id, feb.status, " +
            "fp.full_name AS family_name, fp.phone AS family_phone, fp.relationship AS family_relationship, " +
            "ep.full_name AS elderly_name, ep.id_card_number AS elderly_id_card " +
            "FROM family_elderly_bindings feb " +
            "LEFT JOIN family_profiles fp ON feb.family_user_id = fp.user_id " +
            "LEFT JOIN elderly_profiles ep ON feb.elderly_user_id = ep.user_id " +
            "WHERE feb.elderly_user_id = #{elderlyUserId}")
    Page<BindingInfo> selectSelfBindingElderlyPaged(Page<BindingInfo> page, @Param("elderlyUserId") Long elderlyUserId);


}
